﻿Imports System.Runtime.Serialization
Imports System.IO
'保存断面的观测点信息

<Serializable()> Public Class TunnelSectionPoints

    Public Points As List(Of SectionPoint) '所有观测点
    Public InstrHeight As Double = 0 '仪器高
    Public SectionNo As String = "" '断面号
    Public SectionLen As String = "" '断面里程
    Public StdErr As Double         '该组点拟合出来圆的中误差

    Public Sub New()
        Points = New List(Of SectionPoint)
    End Sub

    Public center As TSPCoor
    Public start_hv As Double
    Public end_hv As Double
    Public Radius As Double
    Public F_Note As Boolean = False
    Public Function Get_Clone() As Object
        Return Nothing
    End Function
    Function Read(ByVal FileName As String) As Boolean
        Dim sData As String = ""
        Try
            If ReadFile(FileName, sData) Then
                Dim Lines() As String = sData.Split(vbCrLf)
                Dim StationSetPassed As Boolean = False
                Dim dre As FileInfo = New FileInfo(FileName)
                Me.SectionNo = Expand_Name(dre.Name)
                For Each Line As String In Lines
                    Line = Line.Trim
                    If Line.Length > 0 Then
                        Dim v() As String = Line.Split(",")
                        If v.Length = 8 Then
                            If Not StationSetPassed Then
                                Me.InstrHeight = CDbl(v(7))
                                Me.SectionLen = v(4)
                                StationSetPassed = True
                            End If
                            Dim vP As New SectionPoint(v(0), SvAngle.DmsToRad(Convert.ToDouble(v(1))).ToString(), v(2))
                            Points.Add(vP)
                        End If
                    End If
                Next
                CalculateCoor(New TSPCoor(0, 0))
            Else
                Return False
            End If
        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function
    Function Expand_Name(ByVal name As String) As String
        name = name.Split(".")(0)
        Return name
    End Function
    Private Function ReadFile(ByVal FullFileName As String, ByRef sData As String) As Boolean
        Try
            Dim FN As System.IO.TextReader = New IO.StreamReader(FullFileName)
            sData = FN.ReadToEnd
            FN.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
            Return False
        End Try
        Return True
    End Function

    Sub CalculateCoor(ByVal BaseAtPoint As TSPCoor)
        For Each vP As SectionPoint In Points
            vP.CalculateCoor(BaseAtPoint)
        Next
    End Sub

    Overrides Function ToString() As String
        Dim sTmp As String = String.Empty
        For Each vp As SectionPoint In Points
            sTmp &= vp.ToString() & ","
            sTmp &= String.Format("{0,5}", SectionNo) & ","
            sTmp &= String.Format("{0,8}", SectionLen) & ","
            sTmp &= vp.Coor.ToString & ","
            sTmp &= String.Format("{0,6}", String.Format("{0:0.000}", InstrHeight)) & ","
            Dim az As Double = SvAngle.RadToDms(vp.AzimFromCenter)
            sTmp &= String.Format("{0,9}", String.Format("{0:0.0000}", az)) & vbCrLf
        Next
        Return sTmp
    End Function


    Function FindNear180(ByRef vP_Near180U As TSPCoor, ByRef vP_Near180L As TSPCoor) As Boolean
        Try
            Dim Hv180U As Double = 360
            Dim NoU As Integer = -1
            Dim Hv180L As Double = 0
            Dim NoL As Integer = -1

            For i As Integer = 0 To Points.Count - 1
                Dim vp As SectionPoint = Points.Item(i)
                If vp.Hv > 180 Then
                    If vp.Hv < Hv180U Then
                        Hv180U = vp.Hv
                        NoU = i
                    End If

                Else
                    If vp.Hv > Hv180L Then
                        Hv180L = vp.Hv
                        NoL = i
                    End If

                End If
            Next

            vP_Near180U = Points(NoU).Coor
            vP_Near180L = Points(NoL).Coor

        Catch ex As Exception
            Return False
        End Try

        Return True

    End Function


End Class

